package com.example.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.myapplication.R;
import com.example.myapplication.activity.HappyActivity;
import com.example.myapplication.activity.ShYuActivity;
import com.example.myapplication.activity.ShunDeActivity;
import com.example.myapplication.bean.MoneyData;
import com.example.myapplication.common.Common;

import java.util.List;

/**
 * @author ：xiongwei
 * @date ：Created in 2021/7/18 21:39
 */
public class MonthListAdapter extends BaseAdapter {


    private Context context;
    private List<MoneyData> moneyDataList;

    public MonthListAdapter() {

    }

    public MonthListAdapter(Context context, List<MoneyData> moneyDataList) {
        this.context = context;
        this.moneyDataList = moneyDataList;
    }

    @Override
    public int getCount() {
        return moneyDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return moneyDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /** 在不重写getView 方法的情况下，每次调用 getView 时都要重新实例化子项 item 的布局文件，然后通过 findViewById 重新寻找 View 组件并绘画
         着会导致两个问题，1. 重复加载布局文件；2. 重复多次寻找 View 组件
         解决：使用 convertView 作为 View 缓存，形成 adapter 的itemView 重用机制，减少重绘 view 的次数
         形参 view 中缓存了itemView 的布局文件
         */
        View view;
        ViewHolder holder;

        if (convertView == null) {
            //　LayoutInflater 用于加载布局的系统服务，实例化与Layout XML文件对应的View对象
            // 不能直接使用， 需要通过getLayoutInflater( )方法或getSystemService( )方法来获得与当前Context绑定的 LayoutInflater实例。
            LayoutInflater factory = LayoutInflater.from(context);
            // reSource:View 的layout 的ID
            // root： 生成的view 对象的父控件。若提供了 root（！null），则返回 root 作为根结点，否则，返回 view 对象的根布局作为根布局，
            view = factory.inflate(R.layout.list_view_month, null);
            // 2. 使用 ViewHolder 实现 View 组件的缓存
            // 重用 View 时就不用通过 findViewById 重新寻找 view 组件，同时减少 view 组件重绘的次数
            holder = new ViewHolder();
            holder.happyParkButton = (Button) view.findViewById(R.id.listview_happy_pack_button);
            holder.shYuButton = (Button) view.findViewById(R.id.listview_sh_yu_button);
            holder.shundeButton = (Button) view.findViewById(R.id.listview_shun_de_button);
            holder.dayText = (EditText) view.findViewById(R.id.listview_day_editText);
            holder.startTimeText = (EditText) view.findViewById(R.id.listview_start_time_editText);
            holder.endTimeText = (EditText) view.findViewById(R.id.listview_end_time_editText);
            holder.moneyText = (EditText) view.findViewById(R.id.listview_money_editText);

            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        // 2. 使用 ViewHolder 实现 View 组件的缓存
        // 重用 View 时就不用通过 findViewById 重新寻找 view 组件，同时减少 view 组件重绘的次数
        MoneyData moneyData = moneyDataList.get(position);
        holder.dayText.setText(moneyData.getDayTime());
        holder.startTimeText.setText(moneyData.getStartTime());
        holder.endTimeText.setText(moneyData.getEndTime());
        holder.moneyText.setText(moneyData.getMoney());

        holder.happyParkButton.setTag(holder);
        holder.shYuButton.setTag(holder);
        holder.shundeButton.setTag(holder);
        holder.happyParkButton.setOnClickListener(btnOnClickListener);
        holder.shYuButton.setOnClickListener(btnOnClickListener);
        holder.shundeButton.setOnClickListener(btnOnClickListener);

        return view;
    }

    View.OnClickListener btnOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewHolder holder = (ViewHolder) v.getTag();
            MoneyData moneyData = new MoneyData();
            moneyData.setStartTime(holder.startTimeText.getText().toString().trim());
            moneyData.setEndTime(holder.endTimeText.getText().toString().trim());
            moneyData.setDayTime(holder.dayText.getText().toString().trim());
            moneyData.setMoney(holder.moneyText.getText().toString().trim());

            switch (v.getId()) {
                case R.id.listview_happy_pack_button:
                    moneyData.setActivityValue(Common.HAPPY_PARK);
                    startActivity(context, HappyActivity.class, moneyData);
                    break;
                case R.id.listview_sh_yu_button:
                    moneyData.setActivityValue(Common.SHAN_YU);
                    startActivity(context, ShYuActivity.class, moneyData);
                    break;
                case R.id.listview_shun_de_button:
                    moneyData.setActivityValue(Common.SHUN_DE);
                    startActivity(context, ShunDeActivity.class, moneyData);
                    break;
                default:
                    Toast.makeText(context, "没对象，好好找对象吧", Toast.LENGTH_SHORT).show();
            }

        }
    };

    class ViewHolder {
        Button happyParkButton;
        Button shYuButton;
        Button shundeButton;
        private EditText startTimeText;
        private EditText endTimeText;
        private EditText moneyText;
        private EditText dayText;
    }

    private void startActivity(Context packageContext, Class<?> cls, MoneyData moneyData) {
        Intent intent = new Intent(packageContext, cls);
        intent.putExtra("moneyData", moneyData);
        packageContext.startActivity(intent);
    }

}

